package com.job.common.jwt;

import com.job.model.Login.Login;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lisa on 2019/1/14.
 */
@Component
public class JwtTokenUtil {

    //    @Value("${jwt.secret}")
//    private String secret;
//
//    @Value("${jwt.expiration}")
//    private Long expiration;
    private String secret = "job-manage";
    //24小时
    //private int expiration = 86400;
    //24000小时
    private int expiration = 864000000;

    public String generateToken(Login login) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("aeId", login.getId());
        claims.put("aiId", login.getAiId());
        claims.put("timestamp", System.currentTimeMillis());
        return Jwts.builder()
                .setClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis() + expiration * 1000))
                // 不使用公钥私钥
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }

    public Claims parseToken(String token) {
        try {
            Claims claims = Jwts.parser()
                    // 不使用公钥私钥
                    .setSigningKey(secret)
                    .parseClaimsJws(token).getBody();
            return claims;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
